/**
 * 
 */
package controller;

import helpers.IDGenerator;
import listeners.AddListener;
import listeners.DeleteSelListener;
import listeners.LoadListener;
import listeners.SaveListener;
import listeners.SerializationListener;
import model.ModelShop;
import model.Product;
import model.ProductList;
import serialisierung.BinaryStrategy;
import serialisierung.OpenJPAStrategy;
import serialisierung.Serialisierung;
import serialisierung.XMLStrategy;
import serialisierung.XStreamStrategy;
import view.ViewShop;

public class ControllerShop implements AddListener, DeleteSelListener,
		SerializationListener, LoadListener, SaveListener {

	private ModelShop modelShop;
	private ViewShop viewShop;

	// Die einzelnen Strategien werden hier einmalig erzeugt
	BinaryStrategy binSer = new BinaryStrategy();
	XMLStrategy beanSer = new XMLStrategy();
	XStreamStrategy xStreamSer = new XStreamStrategy();
	OpenJPAStrategy openJPASer = new OpenJPAStrategy();
	// TODO: Klären, ob es hier sinnvoller ist, die OpenJPA-Strategie
	// erst zu erzeugen, wenn sie ausgewählt wurde, da sonst IMMER beim
	// Programmstart eine Verbindung zur DB hergestellt wird, egal ob
	// der Nutzer überhaupt auf die DB zugreifen will
	Serialisierung serialisierung = new Serialisierung();

	public ControllerShop() {

	}

	public void link(ModelShop model, ViewShop view) {

		// Model anmelden
		this.modelShop = model;
		this.modelShop.addObserver(view);

		// Listener hinzufügen
		this.viewShop = view;
		viewShop.addDeleteSelListener(this);
		viewShop.addAddListener(this);
		viewShop.addLoadListener(this);
		viewShop.addSaveListener(this);
		viewShop.addSerializationListener(this);

		viewShop.update(modelShop, null);

	}

	@Override
	public void deleteSel() {
		if (viewShop.getSelected() != null) {
			for (fpt.com.Product p : viewShop.getSelected()) {
				modelShop.delete(p);
			}

		}

	}

	// wird nur erstellt und hinzugefügt, wenn gültige ID verfügbar
	@Override
	public void add(String name, double price, int quantity) {

		try {
//			long id = IDGenerator.generateID(modelShop);
			modelShop.add(createProduct(name, price, quantity));
		} catch (Exception e) {
			System.err.println(e.getMessage());
		}

	}

	@Override
	public void chooseSerialization(String serializationType) {
		switch (serializationType) {

		case "binary":
			System.out.println("binary chosen");
			serialisierung.setStrat(binSer);
			viewShop.activateLoadSave(true);
			break;
		case "beans":
			System.out.println("beans chosen");
			serialisierung.setStrat(beanSer);
			viewShop.activateLoadSave(true);
			break;
		case "xstream":
			System.out.println("xstream chosen");
			serialisierung.setStrat(xStreamSer);
			viewShop.activateLoadSave(true);
			break;
		case "openjpa":
			System.out.println("openjpa chosen");
			serialisierung.setStrat(openJPASer);
			viewShop.activateLoadSave(true);
			break;
		default:
			System.out.println("none chosen");
			serialisierung.setStrat(null);
			viewShop.activateLoadSave(false);
			break;
		}
	}

	@Override
	public void save() {
		serialisierung.writeProductList(modelShop.getProductList());

	}

	@Override
	public void load() {
		ProductList prodList = serialisierung.readProductList();

		System.out.println("Deserialisiert:");

		for (fpt.com.Product p : prodList) {
			System.out.println("id: " + p.getId() + " name: " + p.getName()
					+ " price: " + p.getPrice() + " quant: " + p.getQuantity());
		}

		modelShop.setProductList(prodList);
	}

	private Product createProduct(String name, double price, int quantity) {

		Product product = new Product();
		product.setName(name);
		//product.setId(id);
		product.setPrice(price);
		product.setQuantity(quantity);
		System.out.println("Create new Product - name: " + name 
				+ " price: " + price + " quantity: " + quantity);
		return product;
	}

}
